package com.inpion.biz.broker.service.util;

import java.util.Set;

import com.inpion.biz.broker.service.data.DataStore;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import kr.co.redcircle.broker.Broker;
import kr.co.redcircle.broker.io.BrokerResult;

public class DataResultPrinter {

	public static void confirm(BrokerResult brokerResult) {
		
		/* broker confirm */
		JSONObject resultJsonObject = JSONObject.fromObject(brokerResult.toJSONObject().get(Broker.RESULT) );
		System.out.println("resultJSONObject: " + resultJsonObject);
		
		/* data confirm */
		dataConfirm(resultJsonObject);
		
		/* meta data confirm */
		metaConfirm(resultJsonObject);
	}

	@SuppressWarnings("unchecked")
	private static void metaConfirm(JSONObject resultJsonObject) {
		JSONObject resultJsonMeta = resultJsonObject.getJSONObject(DataStore.METADATA);
		System.out.println("resultJsonMeta: " + resultJsonMeta);

		Set<String>keySet = resultJsonMeta.keySet();
		
		for(String key : keySet) {
			System.out.println("{\"" + key + "\":\"" + resultJsonMeta.getString(key) + "\"]");
		}
	}

	private static void dataConfirm(JSONObject resultJsonObject) {
		JSONArray resultJsonArray = resultJsonObject.getJSONArray(DataStore.RESULT);
		System.out.println("resultJsonArray: " + resultJsonArray);
		
		for(int i=0; i<resultJsonArray.size(); i++) {
			JSONArray row = resultJsonArray.getJSONArray(i);
			for(int j=0; j<row.size(); j++) {
				System.out.println("[" + i + "][" + j + "] : " + row.get(j) );
			}
		}
	}
}
